<HTML><HEAD><TITLE>term_string(?Term, ?String)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Term Manipulation</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>term_string(?Term, ?String)</H1>
Conversion between a Prolog term and a string.


<DL>
<DT><EM>Term</EM></DT>
<DD>Prolog term.
</DD>
<DT><EM>String</EM></DT>
<DD>String or a variable.
</DD>
</DL>
<H2>Description</H2>
   If String is instantiated, its contents are parsed, and if the whole
   string can be parsed as one Prolog term it is unified with Term.  If
   String is not instantiated, Term is written into a string (using
   writeq/2) and String is bound to it.

<P>
   To change the way the term is converted into a string, e.g.  to include
   attributed variable print handlers, it is possible to define it as follows:

<P>
<PRE>
term_to_string(T, S) :-
    open(string(""), write, Stream),
    % use the flags which you want
    printf(Stream, "%mw", [T]),
    get_stream_info(Stream, name, S),
    close(Stream).
</PRE>

<H3>Modes and Determinism</H3><UL>
<LI>term_string(?, -) is det
<LI>term_string(-, +) is det
</UL>
<H3>Modules</H3>
This predicate is sensitive to its module context (tool predicate, see @/2).
<H3>Exceptions</H3>
<DL>
<DT><EM>(5) type error </EM>
<DD>String is instantiated, but not to a string.
<DT><EM>(7) string contains unexpected characters </EM>
<DD>String cannot be converted to a Prolog term.
</DL>
<H2>Examples</H2>
<PRE>
Success:
      term_string(T, "look").      (gives T=look).
      term_string(T, "26.0").      (gives T=26.0).
      term_string(T, "f(1,2).").   (gives T=f(1,2)).
      term_string(T, "f(1,2)").    (gives T=f(1,2)).
      term_string(f(1,2),L).       (gives L="f(1, 2)").
      term_string(f(1,2),"f(1, 2)").
      term_string(atom,S).         (gives S="atom").
      term_string(.(a,.(1,[])),S). (gives S="[a, 1]").
      term_string(2.60,"2.6").
      term_string(2.6,"2.60").
      term_string(T,S).            (gives T=_g94; S="_g94").

Fail: term_string(2.6,"2.5").

Error:
      term_string(T,atom).              (Error 5).
      [eclipse]: term_string(T,"F(1,2)").  % String not a string
      F(1,2)                               % of a prolog term
       ^ (here?)
      syntax error: unexpected token
      string contains unexpected characters in term_string(T, "F(1,2)")



</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/stratom/integer_atom-2.html">integer_atom / 2</A>, <A HREF="../../kernel/stratom/number_string-2.html">number_string / 2</A>, <A HREF="../../kernel/stratom/string_list-2.html">string_list / 2</A>, <A HREF="../../kernel/stratom/split_string-4.html">split_string / 4</A>
</BODY></HTML>
